<template>
  <el-dialog title="新增员工" :visible="showDialog" :before-close="handleClose" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="formData" :model="formDatas" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formDatas.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formDatas.mobile" style="width:50%" placeholder="请输入手机" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formDatas.timeOfEntry" type="date" placeholder="请输入日期" style="width:50%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formDatas.formOfEmployment" style="width:50%" placeholder="请选择" @blur="checkManager">
          <el-option v-for="item in employeesEnum.informaltype" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formDatas.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          ref="inp"
          v-model="formDatas.departmentName"
          style="width:50%"
          placeholder="请输入部门"
          @focus="showTree = true"
        />
        <!-- 放置一个树形组件 -->
        <el-tree
          v-if="showTree"
          class="departmentList"
          :expand-on-click-node="false"
          :data="treeData"
          :props="{label:'name'}"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formDatas.correctionTime" type="date" placeholder="请选择时间" style="width:50%" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="sumbit">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDeparments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // 聘用形式
    employeesEnum: {
      type: Object,
      default: () => {},
      require: true
    }
  },
  data() {
    return {
      formDatas: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [], // 树形数据
      showTree: false // 控制树形的显示或者隐藏
    }
  },
  created() {
    this.getDeparments()
  },
  methods: {
    // 聘用形式验证
    checkManager() {
      // 对部分表单字段进行校验的方法
      this.$refs.formData.validateField('formOfEmployment')
    },
    // 由于员工的部门是从树形部门中选择一个部门，所以取出组织架构全部信息转化成树型拿到员工部门
    async getDeparments() {
      const { depts } = await getDeparments()
      this.treeData = this.listParts(depts, '')
    //   console.log(this.treeData)
    },
    // 递归找出父级部门的所属子部门
    listParts(list, tolpid) {
      const arr = []
      list.forEach(item => {
        // 找到pid为''的第一级
        if (item.pid === tolpid) {
          // 找到之后 就要去找 item 下面有没有子节点
          // 再次调用自己 将id跟pid匹配 相同则追加进数组
          item.children = this.listParts(list, item.id)
          arr.push(item)
        }
      })
      return arr
    },
    // 树节点被点击时的回调
    selectNode(node, data) {
    //   console.log('node:', node)
    //   console.log('data:', data)
      this.formDatas.departmentName = node.name
      this.showTree = false
    },
    // 新增
    sumbit() {
      this.$refs.formData.validate(vail => {
        if (vail) {
          this.$emit('addEmployee', this.formDatas, vail)
        }
      })
    },
    // 取消
    btnCancel() {
      this.$emit('update:showDialog', false)
      this.$refs.formData.resetFields() // 重置校验字段
    },
    // Dialog的x箭头关闭
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          done()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss">
.departmentList{
  position: absolute;
  width: 50%;
  height: 170px;
  border: 1px solid #ccc;
  overflow: auto;
  z-index: 10;
}
</style>
